package cn.iocoder.yudao.module.javainfo.controller.admin.pet;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.javainfo.controller.admin.pet.vo.*;
import cn.iocoder.yudao.module.javainfo.dal.dataobject.pet.PetDO;
import cn.iocoder.yudao.module.javainfo.service.pet.PetService;

@Tag(name = "管理后台 - 宠物")
@RestController
@RequestMapping("/javainfo/pet")
@Validated
public class PetController {

    @Resource
    private PetService petService;

    @PostMapping("/create")
    @Operation(summary = "创建宠物")
    @PreAuthorize("@ss.hasPermission('javainfo:pet:create')")
    public CommonResult<Long> createPet(@Valid @RequestBody PetSaveReqVO createReqVO) {
        return success(petService.createPet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新宠物")
    @PreAuthorize("@ss.hasPermission('javainfo:pet:update')")
    public CommonResult<Boolean> updatePet(@Valid @RequestBody PetSaveReqVO updateReqVO) {
        petService.updatePet(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除宠物")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('javainfo:pet:delete')")
    public CommonResult<Boolean> deletePet(@RequestParam("id") Long id) {
        petService.deletePet(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除宠物")
                @PreAuthorize("@ss.hasPermission('javainfo:pet:delete')")
    public CommonResult<Boolean> deletePetList(@RequestParam("ids") List<Long> ids) {
        petService.deletePetListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得宠物")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('javainfo:pet:query')")
    public CommonResult<PetRespVO> getPet(@RequestParam("id") Long id) {
        PetDO pet = petService.getPet(id);
        return success(BeanUtils.toBean(pet, PetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得宠物分页")
    @PreAuthorize("@ss.hasPermission('javainfo:pet:query')")
    public CommonResult<PageResult<PetRespVO>> getPetPage(@Valid PetPageReqVO pageReqVO) {
        return success(petService.getPetPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出宠物 Excel")
    @PreAuthorize("@ss.hasPermission('javainfo:pet:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPetExcel(@Valid PetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PetRespVO> list = petService.getPetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "宠物.xls", "数据", PetRespVO.class,
                list);
    }

}